// app.js
import "./utils/message"

App({
    onLaunch() {
        // 展示本地存储能力
        const logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        this.getUserInfo()
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
            }
        })

    },
    async getUserInfo() {
        if (wx.getStorageSync('token')) {
            const result = await wx.$http.get('/userInfo')
            this.globalData.userInfo = result.data.data
        }
    },
    onRouteChange: function (path, query) {
        console.log("路由跳转到：" + path);
        console.log(query)
        // 验证用户是否登录，未登录则跳转到登录页面
        if (!wx.getStorageSync('token') && path !== '/pages/login/login') {
            this.globalData.url = path
            this.globalData.urlData = query
            wx.redirectTo({
                url: '/pages/login/login'
            })
            return false
        }
        return true
    },
    globalData: {
        userInfo: {},
        url: '',
        urlData: {}
    }
})
